<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="日期">
        <el-date-picker v-model="queryParams.dateRange" type="datetimerange" range-separator="至"
          start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
          class="small-date-picker" />
      </el-form-item>

      <el-form-item label="商务信息" prop="ownerId" v-hasPermi="['common:manager:list']">
        <el-select v-model="queryParams.ownerId" clearable placeholder="请选择机构商务">
          <el-option v-for="(item, i) in extData.ownerInfoList || []" :key="i" :label="item.nickname"
            :value="item.userId">
          </el-option>
        </el-select>.
        <div class="."></div>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <!-- <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <template>
      <el-table v-loading="loading" :data="accountInfoHisList">
        <el-table-column label="商务姓名" align="center" prop="ownerName" />
        <el-table-column label="总消耗" align="center" prop="totalChangeAmount">
          <template slot-scope="{ row }">
            <span style="color:blue">{{ row.totalChangeAmount }}</span>
          </template>
        </el-table-column>

        <el-table-column label="代理消耗" align="center" prop="payAmount">
          <template slot-scope="{ row }">
            <span style="color: green">{{ row.payAmount }}</span>
          </template>
        </el-table-column>

        <el-table-column label="实际消耗" align="center">
          <template slot-scope="{ row }">
            <span style="color: red">{{ row.totalChangeAmount - row.payAmount }}</span>
          </template>
        </el-table-column>

        <el-table-column label="明细" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.$index !== 0" type="text" @click="viewBill(scope.row)">明细</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>


    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import { listAccountOwner } from "@/api/account/merchantInfoHis";
import { baseInfoExtData } from "@/api/merchant/baseInfo";
import dayjs from "dayjs";

export default {
  name: "AccountInfoHis",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      extData: {},
      // 总条数
      total: 0,
      // 总账户流水表格数据
      accountInfoHisList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        startDateInfo: null,
        endDateInfo: null,
        dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
      },
      // 表单参数
      form: {},
      // 表单校验

    };
  },
  created() {
    this.getList();
    this.getOptions();
  },
  methods: {
    getOptions() {
      baseInfoExtData().then((response) => {
        this.extData = response.data;
      });
    },
    /** 查询总账户流水列表 */
    getList() {
      this.loading = true;
      const sata = { ...this.queryParams };
      if (sata.dateRange && sata.dateRange.length === 2) {
        const startDate = this.queryParams.dateRange[0];
        const endDateInfo = this.queryParams.dateRange[1];
        sata.startDateInfo = startDate;
        sata.endDateInfo = endDateInfo;
      }
      delete sata.dateRange;
      sata.ownerId = this.$route.query.ownerId;
      listAccountOwner(sata).then(response => {
        this.accountInfoHisList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    viewBill(row) {
      if (row.ownerId) {
        this.$router.push(`/merchant/accounthis?ownerId=${row.ownerId}`);
      } else {
        this.$router.push(`/merchant/accounthis`);
      }

    },
    getDefaultDate() {
      // 获取当天日期，并以 yyyy-MM-dd 格式返回
      const today = new Date();
      const year = today.getFullYear();
      const month = String(today.getMonth() + 1).padStart(2, '0');
      const day = String(today.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryParams.dateRange = [];
      this.queryParams.ownerId = null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>
